import 'dart:async';

import 'package:flutter/material.dart';
import 'package:hockey/jzx_apps.dart';
import 'package:hockey/utils/jzx_permission_util.dart';

import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';

class JzxPhotoSheet {
  /// 上传图片选择弹出框
  static Future<List<AssetEntity>?> show(
    BuildContext context, {
    String? title,
    int maxAssets = 1,
  }) async {
    final complater = Completer<List<AssetEntity>?>();

    Get.bottomSheet(
      Container(
        width: double.infinity,
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(6),
            topRight: Radius.circular(6),
          ),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (title != null)
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 15),
                child: Text(title,
                    textAlign: TextAlign.center,
                    style: myAppColors.text.font_16.bold),
              ),
            if (title != null) JzxGaps.line,
            GestureDetector(
              onTap: () async {
                Get.back();
                0.1.delay();
                final res = await context.camera(maxAssets: maxAssets);
                complater.complete(res != null ? [res] : null);
              },
              behavior: HitTestBehavior.translucent,
              child: Container(
                width: double.infinity,
                padding: const EdgeInsets.symmetric(vertical: 15),
                child: Text("Camera",
                    textAlign: TextAlign.center,
                    style: myAppColors.text.font_16),
              ),
            ),
            JzxGaps.line,
            GestureDetector(
              onTap: () async {
                Get.back();
                0.1.delay();
                final res = await context.picker(maxAssets: maxAssets);
                complater.complete(res);
              },
              behavior: HitTestBehavior.translucent,
              child: Container(
                width: double.infinity,
                padding: const EdgeInsets.symmetric(vertical: 15),
                child: Text("Photo Album",
                    textAlign: TextAlign.center,
                    style: myAppColors.text.font_16),
              ),
            ),
            JzxGaps.boldLine(),
            GestureDetector(
              onTap: () => Navigator.pop(context),
              behavior: HitTestBehavior.translucent,
              child: Container(
                width: double.infinity,
                padding: EdgeInsets.only(
                    top: 10,
                    bottom: 10 + MediaQuery.of(context).padding.bottom),
                margin:
                    const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(5),
                ),
                child: Text("Cancel",
                    textAlign: TextAlign.center,
                    style: myAppColors.text.font_16),
              ),
            ),
          ],
        ),
      ),
      barrierColor: context.appColors.barrierColor,
    );

    return complater.future;
  }
}

extension BuildContextPicker on BuildContext {
  /// 图片选择
  Future<List<AssetEntity>?> picker({
    int maxAssets = 1,
    List<AssetEntity>? selectedAssets,
  }) async {
    final state = await PermissionUtil().requestPhoto();

    if (!state) {
      return null;
    }

    return await AssetPicker.pickAssets(
      this,
      pickerConfig: AssetPickerConfig(
        maxAssets: maxAssets,
        selectedAssets: selectedAssets,
        requestType: RequestType.image,
      ),
    );
  }

  /// 相机
  Future<AssetEntity?> camera({
    int maxAssets = 1,
    List<AssetEntity>? selectedAssets,
  }) async {
    final state = await PermissionUtil().requestCamera();

    if (!state) {
      return null;
    }

    return await CameraPicker.pickFromCamera(
      this,
      pickerConfig: const CameraPickerConfig(),
      locale: const Locale('en', 'US'),
    );
  }
}
